package com.bdqn.newdream.controller;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.bdqn.newdream.javabean.AProblem;
import com.bdqn.newdream.javabean.DreamingUser;
import com.bdqn.newdream.javabean.Imgjson;
import com.bdqn.newdream.javabean.Problemlist;
import com.bdqn.newdream.javabean.ReComment;
import com.bdqn.newdream.pojo.Comment;
import com.bdqn.newdream.pojo.Dreamuser;
import com.bdqn.newdream.pojo.Like;
import com.bdqn.newdream.pojo.Message;
import com.bdqn.newdream.pojo.Problem;
import com.bdqn.newdream.service.Impl.DreamuserServiceImpl;
import com.google.gson.Gson;

@RequestMapping
@Controller
public class DreamuserController {
	/*
	 * Service层对象
	 */
	@Resource(name="dreamuserService")
	private DreamuserServiceImpl dreamuserServiceImpl;
	
	
	
	
	/*
	 * 测试
	 */
	@ResponseBody
	@RequestMapping("/temp")
	public List<Dreamuser> getuser(){
		return dreamuserServiceImpl.getdreamuser();
	}
	/*
	 * 测试页面
	 */
	@RequestMapping("/temp1")
	public String getindex(){
		return "index";
	}
	/*
	 * 测试
	 */
	@ResponseBody
	@RequestMapping("/tempdome")
	public List<Problemlist> ttempfsda(){
		return dreamuserServiceImpl.problemlist(1,1);
	}
	/*
	 * 测试
	 */
	@RequestMapping("/doem")
	public String getdoem(){
		return "doem";
	}
	/*
	 * 测试
	 */
	@ResponseBody
	@RequestMapping("/doem1")
	public AProblem getdoem1(){
		AProblem temp=dreamuserServiceImpl.selectAProblem(6, 33);
		return temp;
	}
	
	@ResponseBody
	@RequestMapping("/doem2")
	public Comment getdoemf(){
		return dreamuserServiceImpl.doem();
		
	}
	@ResponseBody
	@RequestMapping("/doem3")
	public DreamingUser Doem(){
		return dreamuserServiceImpl.loginuser("1160132377", "langzi1998");
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	/*
	 * 翻页请求
	 */
	@RequestMapping(value="/swoppage/{pagenum}")
	public String fengpage(Model model,HttpSession session,@PathVariable(value="pagenum") Integer pagenum){
		dreamuserServiceImpl.loadmain(session, model,pagenum);
		return "main";
	}
	/*
	 * 图片上传的请求(富文本域)
	 */
	@ResponseBody
	@RequestMapping(value="/saveimg")
	public Imgjson fileupload(MultipartFile file,HttpServletRequest request){
		Imgjson js=dreamuserServiceImpl.saveImgjson(file,request);
		return js;
	}
	/*
	 * 发表问题页面
	 */
	@RequestMapping("/add")
	public String getadd(Model model,HttpSession session){
		DreamingUser temp=(DreamingUser)session.getAttribute("user");
		if(temp!=null)
			return "add";
		model.addAttribute("message","请先登录!");
		model.addAttribute("action","gologin");
		return "login";
	}
	/*
	 * 发表问题请求
	 */
	@RequestMapping("/savep")
	public String saveProblem(String content,String title,HttpSession session,Model model){
		if(content!=null&&title!=null&&content.trim().length()>0&&title.trim().length()>0){
			DreamingUser temp=(DreamingUser)session.getAttribute("user");
			if(temp!=null){
				Problem aproblem=new Problem();
				aproblem.setText(content);
				aproblem.setTitle(title);
				aproblem.setUid(temp.getId());
				dreamuserServiceImpl.saveProblem(aproblem);
				dreamuserServiceImpl.loadmain(session, model,1);
			}
		}
		return "main";
	}
	/*
	 * 展示问题的详细信息
	 */
	@RequestMapping("/detail/{id}")
	public String detailproblem(@PathVariable(value="id") Integer id,Model model,HttpSession session){
		DreamingUser temp=(DreamingUser)session.getAttribute("user");
		if(temp!=null)
			model.addAttribute("aprobleam",dreamuserServiceImpl.selectAProblem(id,temp.getId()));
		else
			model.addAttribute("aprobleam",dreamuserServiceImpl.selectAProblem(id));
		model.addAttribute("firstnum",dreamuserServiceImpl.Firstnum(1));
		model.addAttribute("firstcount", dreamuserServiceImpl.Firstcount(1));
		return "detail";
	}
	/*
	 * 问题页面
	 */
	@RequestMapping("/main")
	public String getmain(Model model,HttpSession session){
		dreamuserServiceImpl.loadmain(session, model,1);
		return "main";
	}
	/*
	 * 问题列表的json数据
	 */
	@ResponseBody
	@RequestMapping("/problem")
	public List<Problemlist> getProblem(){
		return dreamuserServiceImpl.getProblemlist(1,33,1);
	}
	/*
	 * 登录页面
	 */
	@RequestMapping("/login")
	public String getlogin(Model model){
		model.addAttribute("action","gologin");
		return "login";
	}
	/*
	 * 登录请求
	 */
	@RequestMapping("/gologin")
	public String selectlogin(String userName,String pass,HttpSession httpSession,Model model){
		DreamingUser user=dreamuserServiceImpl.loginuser(userName, pass);
		if(user!=null&&user.getId()!=null){
			httpSession.setAttribute("user",user);
			user.setUsername(userName);
			model.addAttribute("problem",dreamuserServiceImpl.getProblemlist(1,user.getId(),1));
			model.addAttribute("page", dreamuserServiceImpl.countProblem());
			model.addAttribute("thispage",1);
			return "main";
		}
		model.addAttribute("message","用户名或密码错误!");
		model.addAttribute("action","gologin");
		return "login";
	}
	/*
	 * 注销请求
	 */
	@RequestMapping("/quit")
	public String getquit(HttpSession httpSession){
		httpSession.setAttribute("user",null);
		return "redirect:login";
	}
	
	
	/*
	 * 取消顶置的请求
	 */
	@RequestMapping("/didt/{id}")
	public String delectCollect(@PathVariable(value="id") Integer id,Integer pid,HttpSession session,Model model){
		DreamingUser temp=(DreamingUser)session.getAttribute("user");
		if(temp!=null){
			dreamuserServiceImpl.deleteByindividuation(id);
			model.addAttribute("aprobleam",dreamuserServiceImpl.selectAProblem(pid,temp.getId()));
		}else{
			model.addAttribute("aprobleam",dreamuserServiceImpl.selectAProblem(pid));
		}
		model.addAttribute("firstnum",dreamuserServiceImpl.Firstnum(1));
		model.addAttribute("firstcount", dreamuserServiceImpl.Firstcount(1));
		return "detail";
	}
	/*
	 * 顶置的请求
	 */
	@RequestMapping("/sidt/{id}")
	public String insertCollect(@PathVariable(value="id") Integer pid,HttpSession session,Model model){
		DreamingUser temp=(DreamingUser)session.getAttribute("user");
		if(temp!=null){
			if(dreamuserServiceImpl.selectsaveiid(temp.getId(), pid)==null){
				dreamuserServiceImpl.insertindividuation(pid, temp.getId());
			}
			model.addAttribute("aprobleam",dreamuserServiceImpl.selectAProblem(pid,temp.getId()));
		}else{
			model.addAttribute("aprobleam",dreamuserServiceImpl.selectAProblem(pid));
		}
		model.addAttribute("firstnum",dreamuserServiceImpl.Firstnum(1));
		model.addAttribute("firstcount", dreamuserServiceImpl.Firstcount(1));
		return "detail";
	}
	/*
	 * 删除问题的请求
	 */
	@RequestMapping("/dprobleam/{id}")
	public String dprobleam(@PathVariable(value="id") Integer id,HttpSession session,Model model){
		DreamingUser temp=(DreamingUser)session.getAttribute("user");
		if(temp!=null){
			dreamuserServiceImpl.delectAProbleam(id);
			model.addAttribute("aprobleam",dreamuserServiceImpl.selectAProblem(id,temp.getId()));
		}else{
			model.addAttribute("message","登录超时!");
			return "login";
		}
		dreamuserServiceImpl.loadmain(session, model,1);
		return "main";
	}
	/*
	 * 收藏问题的请求
	 */
	@RequestMapping("/scollect/{id}")
	public String savecollect(@PathVariable(value="id") Integer pid,HttpSession session,Model model){
		DreamingUser temp=(DreamingUser)session.getAttribute("user");
		if(temp!=null){
			if(dreamuserServiceImpl.selectcid(temp.getId(), pid)==null){
				dreamuserServiceImpl.selectsavecollect(temp.getId(), pid);
			}
			model.addAttribute("aprobleam",dreamuserServiceImpl.selectAProblem(pid,temp.getId()));
		}else{
			model.addAttribute("message","登录超时!");
			return "login";
		}
		model.addAttribute("firstnum",dreamuserServiceImpl.Firstnum(1));
		model.addAttribute("firstcount", dreamuserServiceImpl.Firstcount(1));
		return "detail";
	}
	/*
	 * 取消收藏
	 */
	@RequestMapping("/dcollect/{id}")
	public String dorpcollect(@PathVariable(value="id")Integer id,Integer pid,HttpSession session,Model model){
		DreamingUser temp=(DreamingUser)session.getAttribute("user");
		if(temp!=null){
			dreamuserServiceImpl.dropcollect(id);
			model.addAttribute("aprobleam",dreamuserServiceImpl.selectAProblem(pid,temp.getId()));
		}else{
			model.addAttribute("message","登录超时!");
			return "login";
		}
		model.addAttribute("firstnum",dreamuserServiceImpl.Firstnum(1));
		model.addAttribute("firstcount", dreamuserServiceImpl.Firstcount(1));
		return "detail";
	}
	/*
	 * 删除评论
	 */
	@RequestMapping("/dcomment/{id}")
	public String dorpcomment(@PathVariable(value="id")Integer id,Integer pid,HttpSession session,Model model){
		DreamingUser temp=(DreamingUser)session.getAttribute("user");
		if(temp!=null){
			dreamuserServiceImpl.dropcomment(id);
			model.addAttribute("aprobleam",dreamuserServiceImpl.selectAProblem(pid,temp.getId()));
		}else{
			model.addAttribute("message","登录超时!");
			return "login";
		}
		model.addAttribute("firstnum",dreamuserServiceImpl.Firstnum(1));
		model.addAttribute("firstcount", dreamuserServiceImpl.Firstcount(1));
		return "detail";
	}
	/*
	 * 评论
	 */
	@RequestMapping(value="/addcomment/{id}",produces = "text/plain;charset=utf-8")
	public String addcomment(String content,@PathVariable(value="id")Integer pid,HttpSession session,Model model){
		
		DreamingUser temp=(DreamingUser)session.getAttribute("user");
		if(temp!=null){
			dreamuserServiceImpl.savecomment(temp.getId(),pid,content, 1);
			model.addAttribute("aprobleam",dreamuserServiceImpl.selectAProblem(pid,temp.getId()));
		}else{
			model.addAttribute("action", "reaction/"+pid);
			model.addAttribute("text", content);
			model.addAttribute("message","请先登录!");
			return "login";
		}
		model.addAttribute("firstnum",dreamuserServiceImpl.Firstnum(1));
		model.addAttribute("firstcount", dreamuserServiceImpl.Firstcount(1));
		return "detail";
	}
	/*
	 * 未登录的评论转登录
	 */
	@RequestMapping(value="/reaction/{id}",produces = "text/plain;charset=utf-8")
	public String relogin(@PathVariable(value="id")Integer pid,HttpSession session,String userName,String pass,Model model,HttpServletRequest request,String text){
		DreamingUser user=dreamuserServiceImpl.loginuser(userName, pass);
		if(user!=null&&user.getId()!=null){
			session.setAttribute("user",user);
			model.addAttribute("aprobleam",dreamuserServiceImpl.selectAProblem(pid,user.getId()));
			model.addAttribute("firstnum",dreamuserServiceImpl.Firstnum(1));
			model.addAttribute("firstcount", dreamuserServiceImpl.Firstcount(1));
			model.addAttribute("text",text);
			return "detail";
		}else{
			model.addAttribute("message","用户名或密码错误!");
			return "login";
		}
	}
	/*
	 * 采纳
	 */
	@RequestMapping("/saveaccept/{id}")
	public String saveaccept(@PathVariable(value="id")Integer cid,Integer pid,HttpSession session,Model model){
		DreamingUser temp=(DreamingUser)session.getAttribute("user");
		if(temp!=null){
			if(dreamuserServiceImpl.selectaccept(pid, cid)==null){
				dreamuserServiceImpl.saveaccept(pid, cid);
			}
			model.addAttribute("aprobleam",dreamuserServiceImpl.selectAProblem(pid,temp.getId()));
		}else{
			model.addAttribute("message","登录超时!");
			return "login";
		}
		model.addAttribute("firstnum",dreamuserServiceImpl.Firstnum(1));
		model.addAttribute("firstcount", dreamuserServiceImpl.Firstcount(1));
		return "detail";
	}
	
	/*
	 * 点赞
	 */
	@ResponseBody
	@RequestMapping("/saveLike")
	public Integer saveLike(HttpSession session,Integer cid){
		DreamingUser temp=(DreamingUser)session.getAttribute("user");
		if(temp!=null){
			if(dreamuserServiceImpl.selectlikeid(cid, temp.getId())==null){
				return dreamuserServiceImpl.saveLike(cid, temp.getId());
			}else{
				return 1;
			}
		}else{
			return 2;
		}
		
	}
	
	/*
	 * 取消赞
	 */
	@ResponseBody
	@RequestMapping("/dropLike")
	public Integer dropLike(HttpSession session,Integer cid){
		DreamingUser temp=(DreamingUser)session.getAttribute("user");
		if(temp!=null){
			return dreamuserServiceImpl.dorplike(cid,temp.getId());
		}else{
			return 2;
		}
		
	}
	
	
	/*
	 * 模糊查询
	 */
	@RequestMapping("/like")
	public String getlike(Model model,String like){
		model.addAttribute("problem",dreamuserServiceImpl.likeprobleam(like,1));
		model.addAttribute("page",dreamuserServiceImpl.countlikep(like));
		model.addAttribute("thispage", 1);
		model.addAttribute("like",like);
		model.addAttribute("identifying","标识");
		System.out.println(like);
		return "main";
	}
	/*
	 * 模糊查询结果翻页
	 */
	@RequestMapping("/nextlike/{id}")
	public String nextlike(Model model,String like,@PathVariable(value="id")Integer id){
		model.addAttribute("problem", dreamuserServiceImpl.likeprobleam(like,id));
		model.addAttribute("page",dreamuserServiceImpl.countlikep(like));
		model.addAttribute("thispage", id);
		model.addAttribute("identifying","标识");
		model.addAttribute("like",like);
		return "main";
	}
	/*
	 * 获取验证码
	 */
	@ResponseBody
	@RequestMapping(value="/sendkey",produces = "text/plain;charset=utf-8")
	public String sendkey(String key){
		return dreamuserServiceImpl.sendkey(key);
	}
	/*
	 * 修改密码
	 */
	@RequestMapping("/uppass")
	public String updatepass(String updateuser,String updatepass,String updateyan,Model model){
		if(dreamuserServiceImpl.updatepass(updateuser,updatepass, updateyan)>0){
			model.addAttribute("tishi", "修改密码成功，请记牢密码");
			dreamuserServiceImpl.updatekey(updateuser);
			return "newdream";
		}else{
			model.addAttribute("updateuser", updateuser);
			model.addAttribute("updatepass", updatepass);
			model.addAttribute("tishi3","请不要中途修改用户名!");
			return "login";
		}
	}
	
	/*
	 * 忘记密码
	 */
	@RequestMapping("/updtepass")
	public String uppass(Model model){
		model.addAttribute("tishi", "修改密码成功，请记牢密码");
		return "uppass";
	}
	/*
	 * 注册
	 */
	@RequestMapping("/newdream")
	public String newdream(HttpServletRequest requset,String username,String pass,String mailbox,Model model){
		if(username!=null&&username.trim().length()>0&&pass!=null&&pass.trim().length()>0&&mailbox!=null&&mailbox.trim().length()>0){}
		Dreamuser temp=new Dreamuser();
		temp.setEmail(mailbox);//设置邮箱
		temp.setPwd(pass);//设置密码
		Message temp1=new Message();
		temp1.setNickname(username);
		return dreamuserServiceImpl.newdream(temp,temp1, model, requset);
	}
	
	/*
	 * 激活
	 */
	@RequestMapping("/activate")
	public String activate(String key,Model model){
		if(dreamuserServiceImpl.activate(key)==0){
			model.addAttribute("tishi", "丢失激活码或您已经激活!");
			return "activate";
		}
		model.addAttribute("tishi", "激活成功!");
		return "activate";
	}
	/*
	 * 用户信息展示页面
	 */
	@RequestMapping("/message/{id}")
	public String message(HttpSession session,Model model,@PathVariable(value="id")Integer id){
		DreamingUser temp1=(DreamingUser)session.getAttribute("user");
		DreamingUser temp=dreamuserServiceImpl.gomessage(id);
		Dreamuser user=dreamuserServiceImpl.selectByPrimaryKey(temp.getMessage().getUid());
		if(user!=null){
			temp.setUsername(user.getUsername());
			if(temp!=null){
				temp.getMessage().setUid(id);
				model.addAttribute("dreamuser", temp);
				model.addAttribute("dreamuser1", temp1);
				return "message";
			}
		}
		return "main";
	}
	/*
	 * 跳转更改信息页面
	 */
	@RequestMapping("/upmess")
	public String upmess(HttpSession session,Model model){
		DreamingUser temp1=(DreamingUser)session.getAttribute("user");
		if(temp1!=null){
			model.addAttribute("dreamuser",temp1);
			return "upmess";
		}else{
			
		}
		model.addAttribute("action","gologin");
		model.addAttribute("message","登录超时!");
		return "login";
	}
	/*
	 * 更改用户信息
	 */
	@RequestMapping(value="/savemess",method = RequestMethod.POST)
	public String savemess(@RequestParam(value = "fileuser", required = false)MultipartFile file,HttpSession session,String userName,String Real_name,String sex,Integer age,HttpServletRequest request,Model model){
		DreamingUser temp1=((DreamingUser)session.getAttribute("user"));
		Dreamuser user=dreamuserServiceImpl.selectByPrimaryKey(temp1.getMessage().getUid());
		if(temp1!=null){
			Message message=new Message();
			message.setId(temp1.getMessage().getId());
			message.setUid(temp1.getId());
			message.setSex(sex); 
			message.setAge(age);
			message.setNickname(userName);
			message.setName(Real_name);
			String str=dreamuserServiceImpl.saveImg(file, request);
			message.setImg(str);
			dreamuserServiceImpl.updateByPrimaryKey(message);
			System.out.println(temp1.getMessage().getId());
			System.out.println(str);
			DreamingUser temp=dreamuserServiceImpl.gomessage(message.getId());
			temp.setUsername(user.getUsername());
			model.addAttribute("dreamuser", temp);
			temp1.getMessage().setImg(str);
			return "message";
		}else{
			model.addAttribute("action ","gologin");
			model.addAttribute("message","登录超时!");
			return "login";
		}
		
	}
	
}
